package com.jinyi.wwdeng.utils.dialog

import android.content.Context
import android.content.Intent
import android.net.Uri
import com.jinyi.base.utils.log.HHLog
import com.jinyi.wwdeng.App
import com.jinyi.wwdeng.AppConfigs
import com.jinyi.wwdeng.pro.ProAct
import com.jinyi.wwdeng.ui.user.act.UserFeedbackAct
import com.jinyi.wwdeng.ui.user.contract.UserFeedbackContract
import com.jinyi.wwdeng.utils.event.TrackEventType
import com.jinyi.wwdeng.utils.event.TrackEventUtils
import com.zhi.analysis.util.AppReviewUtil
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/5/10 16:19
 * @Annotation：
 */
object StarDialogContract {

    /**
     * 展示评分
     */
    fun showScoreDialog(context: Context,data: JSONObject,isNeedAdd: Boolean){

        if (data.has("activeWindow") && data.optJSONObject("activeWindow") != null){
            var isShowActiveDialog = false
            val activeWindowObj = data.optJSONObject("activeWindow")
            //需要弹出
            if (activeWindowObj != null && activeWindowObj.optString("switch") == "YES"){
                if (isNeedAdd){//是否需要判断出现次数
                    val totalNum = activeWindowObj.optInt("num")
                    val curtNum = AppConfigs.getSPInt(AppConfigs.SP_KEY.SP_HENECREDIT_DIALOG_ACTIVE_NUM)
                    if (curtNum < totalNum){
                        isShowActiveDialog = true
                        AppConfigs.saveSPData(AppConfigs.SP_KEY.SP_HENECREDIT_DIALOG_ACTIVE_NUM,curtNum+1)
                    }
                } else {
                    isShowActiveDialog = true
                }
            }
            if (isShowActiveDialog){
                showActiveScoreDialog(context,activeWindowObj.optString("content"))
                return
            }
        }


        if (data.has("normalWindow") && data.optJSONObject("normalWindow") != null){
            val normalWindowObj = data.optJSONObject("normalWindow")
            if (isNeedAdd){
                if (normalWindowObj != null && normalWindowObj.optString("switch") == "YES"){
                    val totalNum = normalWindowObj.optInt("num")
                    val curtNum = AppConfigs.getSPInt(AppConfigs.SP_KEY.SP_HENECREDIT_DIALOG_NORMAL_NUM)
                    if (curtNum >= totalNum){
                        return
                    }
                    AppConfigs.saveSPData(AppConfigs.SP_KEY.SP_HENECREDIT_DIALOG_NORMAL_NUM,curtNum+1)
                    showNormalScoreDialog(context,normalWindowObj.optString("content"))
                }
            } else {//个人中心
                showNormalScoreDialog(context,normalWindowObj.optString("content"))
            }
        }
    }

    /**
     * activeWindow
     */
    private fun showActiveScoreDialog(context: Context,tips: String){
        val scoreActiveDialog = ScoreActiveDialog(context,object : IStarDialogCallback{
            override fun completeScoringCount(count: Int) {
                HHLog.e(">>>>>>>>>>评星数量：$count")
                TrackEventUtils.logEvent(TrackEventType.CLICK.CLICK_USER_RATE_US_RATE,count.toString())
                /**
                 * 数量 >= 4  ->  跳转Google 评分 （AppReviewUtil.launchReviewFlow() ）
                 * 数量 < 4   ->  跳转意见反馈  ( UserFeedbackAct )
                 */
                if (count < 4){
                    val intent = Intent(context,UserFeedbackAct::class.java)
                    context.startActivity(intent)
                } else {
                    val act = context as ProAct
                    AppReviewUtil.launchReviewFlow(act, completed = {
                        HHLog.e(">>>>>completed>>>>>>>>>>$it")
                        if (!it){
                            //跳转到应用市场
                            startMarket()
                        }
                    })
                }
            }
        })
        scoreActiveDialog.setCancelable(true)
        scoreActiveDialog.setCanceledOnTouchOutside(false)
        scoreActiveDialog.show()
        scoreActiveDialog.setTips(tips)
    }

    /**
     * normalWindow
     */
    private fun showNormalScoreDialog(context: Context,tips: String){
        val scoreNormalDialog = ScoreNormalDialog(context,object : IStarDialogCallback{
            override fun completeScoringCount(count: Int) {
                HHLog.e(">>>>>>>>>>评星数量：$count")
                TrackEventUtils.logEvent(TrackEventType.CLICK.CLICK_USER_RATE_US_RATE,count.toString())
                /**
                 * 数量 >= 4  ->  跳转Google 评分 （AppReviewUtil.launchReviewFlow() ）
                 * 数量 < 4   ->  跳转意见反馈  ( UserFeedbackAct )
                 */
                if (count < 4){
                    val intent = Intent(context,UserFeedbackAct::class.java)
                    context.startActivity(intent)
                } else {
                    val act = context as ProAct
                    AppReviewUtil.launchReviewFlow(act, completed = {
                        HHLog.e(">>>>>completed>>>>>>>>>>$it")
                        if (!it){
                            //跳转到应用市场
                            startMarket()
                        }
                    })
                }
            }
        })
        scoreNormalDialog.setCancelable(true)
        scoreNormalDialog.setCanceledOnTouchOutside(false)
        scoreNormalDialog.show()
        scoreNormalDialog.setTips(tips)
    }

    /**
     * 跳转到应用商店
     */
    private fun startMarket(){
        val intent = Intent(Intent.ACTION_VIEW)
        intent.data = Uri.parse("https://play.google.com/store/apps/details?id=${App.instance().packageName}")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        App.instance().startActivity(intent)
    }

    interface IStarDialogCallback {
        //返回评星数量
        fun completeScoringCount(count: Int)
    }
}